//Event Study-Reduced Form Models
eststo clear


if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\ES\"
	local plotSaveDir = "Plots\ES\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/ES/"
	local plotSaveDir = "Plots/ES/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
local Spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"
local Spec2Controls = "`Spec1Controls'"
local Spec3Controls = "`Spec2Controls' i.gasUseDummyExtSpec3"
local Spec4Controls = "`Spec2Controls' i.gasUseDummyExtSpec4"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Event Study Table format
local fmtEventStudy = `"cells(b(fmt(3) star) se(par fmt(3))) keep(*boundRelYear*) nocon label compress longtable mtitles("\makecell{No \\ DSCs}" "\makecell{Documented \\ DSCs}" "\makecell{Suspected \\ DSCs}" "\makecell{Potential \\ DSCs}") starlevels(* 0.1 ** 0.05 *** 0.01) addnotes("* p \textless 0.1, ** p \textless 0.05, *** p \textless 0.01" "Note: All models include state and year fixed effects as well as state-level time trends." "All models cluster standard errors  by state.")"'


//Loop through the number ofr dummies +- one year prior to the event
foreach bound of num 6 11{
	
	preserve
	
	//Add Dummy Variables for Event Study
	//Define Bounds
	local lowerBound = -`bound'
	local upperBound = `bound'
	
	//Define the relative time period that we will use
	local relYear = -1

	//Determine the dummy index of the relative year
	local relIndex = 1-`lowerBound'+`relYear'
	
	//Generate New Variable
	gen boundRelYear = yearrelativeend
	//Enforce Bounds
	replace boundRelYear = `lowerBound' if boundRelYear<=`lowerBound'
	replace boundRelYear = `upperBound' if boundRelYear>=`upperBound'
	replace boundRelYear = . if yearrelativeend==.
	
	//Create shifted Bounds
	gen shiftedBoundRelYear = boundRelYear - `lowerBound'+1
	replace shiftedBoundRelYear = `relIndex' if boundRelYear==.



	//Create Dummies
	qui tab boundRelYear, gen(boundRelYear)
	//Figure out which index for the dummy variables is the last one
	local finalInd = `upperBound'-`lowerBound'+1


	// Drop the relative year column for ease of use in the event study.
	qui drop boundRelYear`relIndex'

	//Establish misssing data character
	local missingDataChar = uchar(8224)
	
// 	if `upperBound'==6{
// 		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 13.shiftedBoundRelYear) omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Highway Gas Use" "relative to {&tau}=-1")"'
//
//		
// 		local plotTitle = "Event Study Relative Year Fixed Effects"
// 		local plotSubtitle = "(±`upperBound' Years)"
// 		local plotNote = "-Point estimates and 95% confidence intervals shown"
//		
// 		label define shiftedBoundRelYear 1 "t $\leq$ -6" 2 "t=-5" 3 "t=-4" 4 "t=-3" 5 "t=-2" 6 "t=-1" 7 "t=0" 8 "t=1" 9 "t=2" 10 "t=3" 11 "t=4" 12 "t=5" 13 "t $\geq$ 6"
// 		label values shiftedBoundRelYear shiftedBoundRelYear
// 		local plotNote2 = ""
// 	}
// 	else{
// 		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 23.shiftedBoundRelYear) omitted baselevels xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0) labsize(small)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Highway Gas Use" "relative to {&tau}=-1")"'
//		
//
// 		local plotTitle = ""
// 		local plotSubtitle = "(±`upperBound' Years)"
// 		local plotNote = "Point estimates and 95% confidence intervals shown"
//		
// 		label define shiftedBoundRelYear 1 "t $\leq$ -11" 2 "t=-10" 3 "t=-9" 4 "t=-8" 5 "t=-7" 6 "t=-6" 7 "t=-5" 8 "t=-4" 9 "t=-3" 10 "t=-2" 11 "t=-1" 12 "t=0" 13 "t=1" 14 "t=2" 15 "t=3" 16 "t=4" 17 "t=5" 18 "t=6" 19 "t=7" 20 "t=8" 21 "t=9" 22 "t=10" 23 "t $\geq$ 11"
// 		label values shiftedBoundRelYear shiftedBoundRelYear
// 		local plotNote2 = "-`missingDataChar' denotes a relative year with >1 state missing data"
// 	}
	
	if `upperBound'==6{
		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 13.shiftedBoundRelYear) omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Travel" "relative to {&tau}=-1")"'
		

		
		local plotTitle = "Event Study Relative Year Fixed Effects"
		local plotSubtitle = "(±`upperBound' Years)"
		local plotNote = "-Point estimates and 95% confidence intervals shown"
		
		label define shiftedBoundRelYear 1 "t $\leq$ -6" 2 "t=-5" 3 "t=-4" 4 "t=-3" 5 "t=-2" 6 "t=-1" 7 "t=0" 8 "t=1" 9 "t=2" 10 "t=3" 11 "t=4" 12 "t=5" 13 "t $\geq$ 6"
		label values shiftedBoundRelYear shiftedBoundRelYear
		local plotNote2 = ""
		local es_controls = "boundRelYear1 boundRelYear2 boundRelYear3 boundRelYear4 boundRelYear5 boundRelYear7 boundRelYear8 boundRelYear9 boundRelYear10 boundRelYear11 boundRelYear12 boundRelYear13"
		local plot_order = "boundRelYear2_res boundRelYear3_res boundRelYear4_res boundRelYear5_res . boundRelYear7_res boundRelYear8_res boundRelYear9_res boundRelYear10 boundRelYear11_res boundRelYear12_res"
		mat at = [-5, -4, -3, -2, 0, 1, 2, 3, 4, 5, 10]
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear13_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Gas Use" "relative to {&tau}=-1") xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0))"' //omitted baselevels xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0))
		gen tempx = 5
		gen tempy = 0
		local max_ind = 13
	}
	else{
		local plotFormat = `"vertical yline(0) keep(*shiftedBoundRelYear*) drop(1.shiftedBoundRelYear 23.shiftedBoundRelYear) omitted baselevels xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0) labsize(small)) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Travel" "relative to {&tau}=-1")"'
		
		
		local plotTitle = ""
		local plotSubtitle = "(±`upperBound' Years)"
		local plotNote = "Point estimates and 95% confidence intervals shown"
		
		label define shiftedBoundRelYear 1 "t $\leq$ -11" 2 "t=-10" 3 "t=-9" 4 "t=-8" 5 "t=-7" 6 "t=-6" 7 "t=-5" 8 "t=-4" 9 "t=-3" 10 "t=-2" 11 "t=-1" 12 "t=0" 13 "t=1" 14 "t=2" 15 "t=3" 16 "t=4" 17 "t=5" 18 "t=6" 19 "t=7" 20 "t=8" 21 "t=9" 22 "t=10" 23 "t $\geq$ 11"
		label values shiftedBoundRelYear shiftedBoundRelYear
		local plotNote2 = "-`missingDataChar' denotes a relative year with >1 state missing data"
		local plot_order = "boundRelYear2_res boundRelYear3_res boundRelYear4_res boundRelYear5_res boundRelYear6_res boundRelYear7_res boundRelYear8_res boundRelYear9_res boundRelYear10_res . boundRelYear12_res boundRelYear13_res boundRelYear14_res boundRelYear15_res boundRelYear16_res boundRelYear17_res boundRelYear18_res boundRelYear19_res boundRelYear20_res boundRelYear21_res boundRelYear22_res"
		local es_controls = "boundRelYear1 boundRelYear2 boundRelYear3 boundRelYear4 boundRelYear5 boundRelYear6 boundRelYear7 boundRelYear8 boundRelYear9 boundRelYear10 boundRelYear12 boundRelYear13 boundRelYear14 boundRelYear15 boundRelYear16 boundRelYear17 boundRelYear18 boundRelYear19 boundRelYear20 boundRelYear21 boundRelYear22 boundRelYear23"
		
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear23_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Gas Use" "relative to {&tau}=-1") xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0))"'
		
		gen tempx = 10 
		gen tempy = 0
		local max_ind = 23
	}

	//Preferred Models
	//Spec1
// 	qui reghdfe loghighwaygasuse ib`relIndex'.shiftedBoundRelYear `Spec1Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat' name("gasuserf_Spec1`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL loghighwaygasuse, `es_controls', `Spec1Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("gasuserf_Spec1`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'GasUseRFESSpec1`upperBound'.png", name("gasuserf_Spec1`upperBound'") replace width(`widthPix') // title("No DSCs Spec.")
	eststo Spec1
	
	//Spec2
// 	qui reghdfe loghighwaygasuse ib`relIndex'.shiftedBoundRelYear `Spec2Controls', vce (cluster `clusterVar')  absorb(`absorbVars')
//
// 	coefplot, `plotFormat' name("gasuserf_Spec2`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL loghighwaygasuse, `es_controls', `Spec2Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("gasuserf_Spec2`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'GasUseRFESSpec2`upperBound'.png", name("gasuserf_Spec2`upperBound'") replace width(`widthPix') //title("Documented DSCs Spec.")
	eststo Spec2
	
	//Spec3
// 	qui reghdfe loghighwaygasuse ib`relIndex'.shiftedBoundRelYear `Spec3Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("gasuserf_Spec3`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL loghighwaygasuse, `es_controls', `Spec3Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("gasuserf_Spec3`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'GasUseRFESSpec3`upperBound'.png", name("gasuserf_Spec3`upperBound'") replace width(`widthPix') //title("Suspected DSCs Spec.")
	eststo Spec3
	//Spec4
// 	qui reghdfe loghighwaygasuse ib`relIndex'.shiftedBoundRelYear `Spec4Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("gasuserf_Spec4`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL loghighwaygasuse, `es_controls', `Spec4Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("gasuserf_Spec4`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'GasUseRFESSpec4`upperBound'.png", name("gasuserf_Spec4`upperBound'") replace width(`widthPix') //title("Potential DSCs Spec.")
	eststo Spec4
	
	
	
	//Store Table

	if `upperBound'==6{
		local title = "Gas Use Event Study Results Small Window \label{tab:ESGasUse`upperBound'}"
	}
	else{
		local title = "Gas Use Event Study Results Large Window \label{tab:ESGasUse`upperBound'}"
	}
	
	graph combine gasuserf_Spec1`bound' gasuserf_Spec2`bound' gasuserf_Spec3`bound' gasuserf_Spec4`bound', ycommon
	graph export "`plotSaveDir'GasUseRFESCombined`upperBound'.png", replace width(`widthPix')

	esttab Spec1 Spec2 Spec3 Spec4 using "`tableSaveDir'GasUseRFES`upperBound'.tex", `fmtEventStudy' title(`title') replace
	
	
	//Per-Capita Models
	
	
	if `upperBound'==6{
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear13_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Gas Use" "relative to {&tau}=-1") xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5", angle(0))"' 

	}
	else{
		local plotFormat = `"vertical yline(0) keep(*boundRelYear*) drop(boundRelYear1_res boundRelYear23_res) xtitle("Years Relative to Inspection Removal") ytitle("Proportional Change in Total Gas Use" "relative to {&tau}=-1") xlabel(1 "-10{superscript:`missingDataChar'}" 2 "-9{superscript:`missingDataChar'}" 3 "-8{superscript:`missingDataChar'}" 4 "-7" 5 "-6" 6 "-5" 7 "-4" 8 "-3" 9 "-2" 10 "-1" 11 "0" 12 "1" 13 "2" 14 "3" 15 "4" 16 "5" 17 "6" 18 "7" 19 "8{superscript:`missingDataChar'}" 20 "9{superscript:`missingDataChar'}" 21 "10{superscript:`missingDataChar'}", angle(0))"'
		
	}
	
	
	local Spec3Controls = "`Spec2Controls' i.gasPerCapitaDummyExtSpec3"
	local Spec4Controls = "`Spec2Controls' i.gasPerCapitaDummyExtSpec4"
	
	//Spec1
// 	qui reghdfe logGasUsePerCapita ib`relIndex'.shiftedBoundRelYear `Spec1Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat' name("gasuserf_Spec1PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logGasUsePerCapita, `es_controls', `Spec1Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("gasuserf_Spec1PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'GasUseRFESSpec1PerCapita`upperBound'.png", name("gasuserf_Spec1PerCapita`upperBound'") replace width(`widthPix') // title("No DSCs Spec.")
	eststo Spec1PerCapita
	
	//Spec2
// 	qui reghdfe logGasUsePerCapita ib`relIndex'.shiftedBoundRelYear `Spec2Controls', vce (cluster `clusterVar')  absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("gasuserf_Spec2PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logGasUsePerCapita, `es_controls', `Spec2Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("gasuserf_Spec2PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'GasUseRFESSpec2PerCapita`upperBound'.png", name("gasuserf_Spec2PerCapita`upperBound'") replace width(`widthPix') //title("Documented DSCs Spec.")
	eststo Spec2PerCapita
	
	//Spec3
// 	qui reghdfe logGasUsePerCapita ib`relIndex'.shiftedBoundRelYear `Spec3Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat'  name("gasuserf_Spec3PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logGasUsePerCapita, `es_controls', `Spec3Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("gasuserf_Spec3PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'GasUseRFESSpec3PerCapita`upperBound'.png", name("gasuserf_Spec3PerCapita`upperBound'") replace width(`widthPix') //title("Suspected DSCs Spec.")
	eststo Spec3PerCapita
	//Spec4
// 	qui reghdfe logGasUsePerCapita ib`relIndex'.shiftedBoundRelYear `Spec4Controls', vce (cluster `clusterVar') absorb(`absorbVars')
//
// 	coefplot, `plotFormat' name("gasuserf_Spec4PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white))
	regFWL logGasUsePerCapita, `es_controls', `Spec4Controls', `absorbVars', `clusterVar', _

	coefplot, `plotFormat'  name("gasuserf_Spec4PerCapita`upperBound'") graphregion(color(white)) plotregion(color(white)) order(`plot_order') addplot(scatter tempy tempx, color(navy)) color(navy)
	graph export "`plotSaveDir'GasUseRFESSpec4PerCapita`upperBound'.png", name("gasuserf_Spec4PerCapita`upperBound'") replace width(`widthPix') // title("Potential DSCs Spec.")
	eststo Spec4PerCapita
	
	
	
	//Store Table

	if `upperBound'==6{
		local title = "Gas Use Per-Capita Event Study Results Small Window \label{tab:ESGasUsePerCapita`upperBound'}"
	}
	else{
		local title = "Gas Use Per-Capita Event Study Results Large Window \label{tab:ESGasUsePerCapita`upperBound'}"
	}
	
	graph combine gasuserf_Spec1PerCapita`bound' gasuserf_Spec2PerCapita`bound' gasuserf_Spec3PerCapita`bound' gasuserf_Spec4PerCapita`bound', ycommon
	graph export "`plotSaveDir'GasUseRFESCombinedPerCapita`upperBound'.png", replace width(`widthPix')

	esttab Spec1PerCapita Spec2PerCapita Spec3PerCapita Spec4PerCapita using "`tableSaveDir'GasUseRFESPerCapita`upperBound'.tex", `fmtEventStudy' title(`title') replace
	
	restore
}
